// asyncPackageComponent/components/findPwdPopup/findPwdPopup.js
const app = getApp();
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    findpwsShow: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgcode: "",
    country_code: 0,
    form: {
      country: "86",
      mobile: "",
      mobileErr: "",
      password: "",
      passwordErr: "",
      confirm_password: "",
      confirm_passwordErr: "",
      captcha: ""
    },
    btnText: '获取验证码',
    isclick: false,
    openPopUp: false
  },
  lifetimes: {
    attached() {
      console.log("wwwwwww")
      this.initForm();
      this.getCountryCode();
      this.getimgdata();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    show_openPopUp(){
      this.setData({
        openPopUp: true
      })
    },
    childSelect(e){
      this.setData({
        'form.country':e.detail
      })
    },
    childClose(e){
      this.setData({
        openPopUp:false
      })
    },
    initForm() {
      this.data.form = {
        country: "86",
        mobile: "",
        mobileErr: "",
        password: "",
        passwordErr: "",
        confirm_password: "",
        confirm_passwordErr: "",
        captcha: ""
      }
      this.setData({
        form: this.data.form
      })
    },
    onClose() {
      this.triggerEvent('close', false)
    },
    async verificationCode() {
      if (this.data.btnTag) return
      if (app._isTextEmpty(this.data.form.country)) {
        app.tips("请输入国际区号");
        return;
      }
      if (app._isTextEmpty(this.data.form.mobile)) {
        app.tips("请填写手机号");
        return;
      }

      if (app._isMoblie(this.data.form.mobile)) {
        app.tips("请填写正确的手机号");
        return;
      }

      if (this.data.imgcode && app._isTextEmpty(this.data.form.captcha)) {
        app.tips("请填写图形验证码");
        return;
      }
      wx.showLoading({
        title: '发送中',
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("member.register.sendCodeV2"),
        data: {
          mobile: this.data.form.mobile,
          state: this.data.form.country,
          captcha: this.data.form.captcha,
          code: this.data.form.code,
          reset: "1",
          sms_type: 2,
        }
      })
      wx.hideLoading();
      if (data.result == 1) {
        this.sendCodeAnimation();
      } else {
        this.setData({
          captcha: ""
        })
        app.tips(data.msg)
        this.getimgdata();
      }
    },
    bindInp(e) {
      let {
        key
      } = e.currentTarget.dataset;
      let val = e.detail;
      this.setData({
        ['form.' + key]: val
      })
    },
    async findpwd() {
      if (app._isTextEmpty(this.data.form.mobile)) {
        app.tips('请填写手机号')
        return
      }
      if (app._isTextEmpty(this.data.form.country)) {
        app.tips('请填写区号')
        return
      }
      if (app._isTextEmpty(this.data.form.code)) {
        app.tips('请填写验证码')
        return
      }
      if (app._isTextEmpty(this.data.form.code)) {
        app.tips('请填写验证码')
        return
      }
      if (this.data.imgcode) {
        if (app._isTextEmpty(this.data.form.captcha)) {
          app.tips('图形验证码不能为空')
          return
        }
      }
      if (app._isTextEmpty(this.data.form.password)) {
        app.tips('请填写密码')
        return
      }
      if (app._isTextEmpty(this.data.form.confirm_password)) {
        app.tips('请填写确认密码')
        return
      }
      if (this.data.form.password != this.data.form.confirm_password) {
        app.tips('两次密码不一致')
        return
      }
      if(this.data.isclick) return
      this.data.isclick = true;
      let urlStr = app.getNetAddresss("member.register.changePassword");
      let {
        data
      } = await app._postNetWork({
        url: urlStr,
        data: this.data.form,
      })
      this.data.isclick = false;
      if (data.result) {
        app.tips('修改成功')
        this.onClose();
      } else {
        app.tips(data.msg)
      }
    },
    sendCodeAnimation() {
      const TIME_COUNT = 60;
      if (!this.data.timer) {
        this.data.count = TIME_COUNT;
        wx.showToast({
          title: '验证码已发送'
        });
        this.data.timer = setInterval(() => {
          if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
            this.data.count--;
            this.setData({
              btnText: this.data.count + "s后重新获取"
            })
            this.data.btnTag = true;
          } else {
            this.data.btnTag = false;
            this.setData({
              btnText: "获取验证码"
            })
            clearInterval(this.data.timer);
            this.data.timer = null;
          }
        }, 1000);
      }
    },
    async getimgdata() {
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("home-page.getCaptcha")
      })
      if (data.result) {
        this.setData({
          imgcode: data.data.captcha.img,
        })
        if(data.data.captcha.wx_token){
          wx.setStorageSync('wx_token', data.data.captcha.wx_token)
        }
      }
    },
    async getCountryCode() {
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("member.register.getInviteCode"),
        data: {
          basic_info: 1
        }
      })
      if (data.result) {
        console.log(data.basic_info)
        if (data.basic_info) {
          this.setData({
            themeStyle: data.basic_info.theme_color,
          })
        }
        this.setData({
          country_code: data.data.country_code,
        })
      } else {
        app.tips(data.msg);
      }
    },
  }
})